package com.hddz.acquisition.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hddz.acquisition.R;
import com.hddz.acquisition.model.ArmshaftItem;
import com.hddz.acquisition.model.BaseListItem;
import com.hddz.acquisition.model.IListItem;

public class ArmshaftAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<ArmshaftItem> mListItem;
	private Context mContext;
	private Boolean nodata = false;

	public ArmshaftAdapter(Context context) {
		super();
		this.mContext = context;
		this.mInflater = LayoutInflater.from(context);
		this.mListItem = new ArrayList<ArmshaftItem>();
	}

	public void updateList(List<ArmshaftItem> list) {
		this.mListItem.clear();
		this.mListItem.addAll(list);
		notifyDataSetChanged();
	}

	public void removeItem(String cCode) {
		BaseListItem deleteItem = null;
		for (IListItem item : mListItem) {
			if (((BaseListItem) item).cCode.equals(cCode)) {
				deleteItem = (BaseListItem) item;
				break;
			}
		}
		if (deleteItem != null) {
			this.mListItem.remove(deleteItem);
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListItem.size();
	}

	@Override
	public IListItem getItem(int position) {
		// TODO Auto-generated method stub
		return mListItem.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.view_stock_armshaft_list_item, null);
			holder = new Holder();
			holder.txt_code = (TextView) convertView
					.findViewById(R.id.txt_code);
			holder.txt_machineCode = (TextView) convertView
					.findViewById(R.id.txt_machineCode);
			holder.txt_axisCode = (TextView) convertView
					.findViewById(R.id.txt_axisCode);
			holder.txt_invname = (TextView) convertView
					.findViewById(R.id.txt_invname);
			holder.txt_quantity = (TextView) convertView
					.findViewById(R.id.txt_quantity);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		ArmshaftItem item = mListItem.get(position);
		holder.txt_code.setText(item.cCode);
		holder.txt_machineCode.setText(item.cMachineCode);
		holder.txt_axisCode.setText(item.cAxisCode);
		holder.txt_invname.setText(item.cInvName);
		holder.txt_quantity.setText(item.iQuantity + "");

		return convertView;
	}

	class Holder {
		private TextView txt_code, txt_machineCode, txt_axisCode, txt_invname,
				txt_quantity;
	}
}
